String Methods In Js

Posted on January 19, 2025 by Vishesh Namdev
Python C C++ Javascript Java
Introduction of Strings in JS

String Methods
JavaScript provides a variety of built-in methods to manipulate and work with strings. These methods make it easier to perform operations like searching, slicing, replacing, and transforming strings. Strings are immutable, so these methods return a new string or value without modifying the original string.

Here are some of the most commonly used string methods in JavaScript:

1. Accessing Characters

  • charAt(index): Returns the character at a specific index.
  • charCodeAt(index): Returns the Unicode value of the character at a specific index.
  • let str = "Hello, World!";
    console.log(str.charAt(0)); // "H"
    console.log(str.charCodeAt(0)); // 72 (Unicode of 'H')

    2. Searching Strings

  • indexOf(value):
  • Returns the first index of the specified value, or -1 if not found.
  • lastIndexOf(value):
  • Returns the last index of the specified value.
  • includes(value):
  • Checks if a string contains a specified value; returns true or false.
  • startsWith(value):
  • Checks if a string starts with a specific value. endsWith(value): Checks if a string ends with a specific value.
    let str = "Hello, JavaScript!";
    console.log(str.indexOf("Java")); // 7
    console.log(str.lastIndexOf("o")); // 4
    console.log(str.includes("Script")); // true
    console.log(str.startsWith("Hello")); // true
    console.log(str.endsWith("!")); // true

    3. Extracting Parts of Strings

  • slice(start, end): Extracts a portion of a string and returns it.
  • substring(start, end): Similar to slice, but cannot accept negative indexes.
  • substr(start, length): Extracts a substring based on the starting index and length. (Deprecated but still supported)
  • let str = "JavaScript is fun!";
      console.log(str.slice(0, 10)); // "JavaScript"
      console.log(str.substring(4, 10)); // "Script"
      console.log(str.substr(0, 4)); // "Java"

    4. Modifying Strings

  • toUpperCase(): Converts the string to uppercase.
  • toLowerCase(): Converts the string to lowercase.
  • trim(): Removes whitespace from both ends of a string.
  • trimStart() / trimEnd(): Removes whitespace from the start or end of a string.
  • let str = "  Hello World!  ";
    console.log(str.toUpperCase()); // "  HELLO WORLD!  "
    console.log(str.toLowerCase()); // "  hello world!  "
    console.log(str.trim()); // "Hello World!"
    console.log(str.trimStart()); // "Hello World!  "
    console.log(str.trimEnd()); // "  Hello World!"

    5. Replacing Content

  • replace(value, newValue): Replaces the first occurrence of a value.
  • replaceAll(value, newValue): Replaces all occurrences of a value.
  • let str = "I love JavaScript. JavaScript is amazing!";
    console.log(str.replace("JavaScript", "JS")); // "I love JS. JavaScript is amazing!"
    console.log(str.replaceAll("JavaScript", "JS")); // "I love JS. JS is amazing!"

    6. Splitting and Joining Strings

  • split(delimiter): Splits the string into an array based on a delimiter.
  • concat(value1, value2, ...): Concatenates two or more strings.
  • let str = "apple,banana,cherry";
    let fruits = str.split(","); // ["apple", "banana", "cherry"]
    console.log(fruits);
      
    let str1 = "Hello";
    let str2 = "World";
    console.log(str1.concat(", ", str2, "!")); // "Hello, World!"
    📢Important Note📢